#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <termios.h>
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <string.h>
#include <unistd.h>

#define BAUDRATE B9600
#define MODEMDEVICE "/dev/ttyUSB1"
#define _POSIX_SOURCE 1 /* POSIX compliant source */
#define FALSE 0
#define TRUE 1

char sample_packet_list[][128] = {
	{ 0x23, 0x61, 0x30, 0x30, 0x30, 0x44, 0xc5, 0x40, 0x30, 0x30, 0x30, 0x30, 0x40, 0x5f, 0x5f, 0x48, 0x44, 0x40, 0x48, 0x55, 0x56, 0x49, 0x54, 0x5a, 0x5e, 0x43, 0x44, 0x52, 0x2d, 0x33, 0x31, 0x30, 0x30, 0x5e, 0x56, 0x32, 0x2e, 0x33, 0x30, 0x61, 0x7c, 0x50, 0x30, 0x39, 0x31, 0x31, 0x31, 0x37, 0x2d, 0x30, 0x30, 0x30, 0x30, 0x5e, 0x32, 0x30, 0x30, 0x39, 0x31, 0x31, 0x31, 0x39, 0x40, 0x56, 0x31, 0x2e, 0x31, 0x30, 0x24, 0x0},

	{ 0x23, 0x61, 0x30, 0x30, 0x30, 0x44, 0xa3, 0x40, 0x30, 0x30, 0x30, 0x30, 0x40, 0x5f, 0x5f, 0x42, 0x41, 0x40, 0x20, 0x30, 0x7c, 0x7c, 0x36, 0x34, 0x2e, 0x30, 0x7c, 0x36, 0x30, 0x2e, 0x30, 0x7c, 0x34, 0x30, 0x24, 0 },

	{ 0x23, 0x61, 0x30, 0x30, 0x30, 0x44, 0xc4, 0x40, 0x30, 0x30, 0x30, 0x30, 0x40, 0x46, 0x42, 0x4c, 0x4d, 0x40, 0x46, 0x52, 0x5e, 0x2d, 0x31, 0x2e, 0x37, 0x35, 0x5e, 0x2d, 0x31, 0x2e, 0x30, 0x30, 0x5e, 0x20, 0x20, 0x30, 0x5e, 0x5e, 0x5e, 0x7c, 0x46, 0x4c, 0x5e, 0x2d, 0x31, 0x2e, 0x37, 0x35, 0x5e, 0x2d, 0x31, 0x2e, 0x30, 0x30, 0x5e, 0x20, 0x20, 0x30, 0x5e, 0x5e, 0x5e, 0x7c, 0x46, 0x56, 0x5e, 0x5e, 0x5e, 0x24, 0 },

	{ 0x23, 0x61, 0x30, 0x30, 0x30, 0x44, 0xa8, 0x40, 0x30, 0x30, 0x30, 0x30, 0x40, 0x46, 0x52, 0x46, 0x49, 0x40, 0x46, 0x52, 0x5e, 0x2d, 0x31, 0x2e, 0x37, 0x35, 0x5e, 0x2d, 0x31, 0x2e, 0x30, 0x30, 0x5e, 0x20, 0x20, 0x30, 0x5e, 0x5e, 0x5e, 0x24, 0},

	{ 0x23, 0x61, 0x30, 0x30, 0x30, 0x44, 0xa8, 0x40, 0x30, 0x30, 0x30, 0x30, 0x40, 0x46, 0x4c, 0x46, 0x49, 0x40, 0x46, 0x4c, 0x5e, 0x2d, 0x31, 0x2e, 0x37, 0x35, 0x5e, 0x2d, 0x31, 0x2e, 0x30, 0x30, 0x5e, 0x20, 0x20, 0x30, 0x5e, 0x5e, 0x5e, 0x24, 0}, 

	{ 0x23, 0x61, 0x30, 0x30, 0x30, 0x44, 0x98, 0x40, 0x30, 0x30, 0x30, 0x30, 0x40, 0x46, 0x44, 0x46, 0x49, 0x40, 0x46, 0x44, 0x5e, 0x5e, 0x5e, 0x24, 0},

	{ 0x23, 0x61, 0x30, 0x30, 0x30, 0x44, 0x98, 0x40, 0x30, 0x30, 0x30, 0x30, 0x40, 0x46, 0x56, 0x46, 0x49, 0x40, 0x46, 0x56, 0x5e, 0x5e, 0x5e, 0x24, 0},

	{ 0x23, 0x61, 0x30, 0x30, 0x30, 0x44, 0x9a, 0x40, 0x30, 0x30, 0x30, 0x30, 0x40, 0x4e, 0x52, 0x46, 0x49, 0x40, 0x4e, 0x52, 0x5e, 0x5e, 0x5e, 0x5e, 0x5e, 0x24, 0}, 

	{ 0x23, 0x61, 0x30, 0x30, 0x30, 0x44, 0x9a, 0x40, 0x30, 0x30, 0x30, 0x30, 0x40, 0x4e, 0x4c, 0x46, 0x49, 0x40, 0x4e, 0x4c, 0x5e, 0x5e, 0x5e, 0x5e, 0x5e, 0x24, 0},
	
	{ 0x23, 0x61, 0x30, 0x30, 0x30, 0x44, 0x98, 0x40, 0x30, 0x30, 0x30, 0x30, 0x40, 0x4e, 0x56, 0x46, 0x49, 0x40, 0x4e, 0x56, 0x5e, 0x5e, 0x5e, 0x24, 0},
	
	{ 0x23, 0x61, 0x30, 0x30, 0x30, 0x44, 0xc2, 0x40, 0x30, 0x30, 0x30, 0x30, 0x40, 0x5f, 0x5f, 0x54, 0x52, 0x40, 0x43, 0x44, 0x52, 0x2d, 0x33, 0x31, 0x30, 0x30, 0x28, 0x53, 0x54, 0x44, 0x29, 0x40, 0x48, 0x55, 0x56, 0x49, 0x54, 0x5a, 0x20, 0x43, 0x6f, 0x2e, 0x2c, 0x20, 0x4c, 0x54, 0x44, 0x2e, 0x20, 0x20, 0x2b, 0x38, 0x32, 0x2d, 0x33, 0x31, 0x2d, 0x34, 0x34, 0x32, 0x2d, 0x38, 0x38, 0x36, 0x38, 0x24, 0},
	
};



volatile int STOP=FALSE; 

int main(int argc, char *argv[])
{
	int fd, res;
	struct termios oldtio,newtio;
	char buf[128];
	char input[128];
	int i;

	fd = open(MODEMDEVICE, O_RDWR | O_NOCTTY | O_NDELAY ); 
	if (fd <0) {perror(MODEMDEVICE); exit(-1); }

	printf("non canonical serial read test.\n");
	printf("fd = %d\n", fd);

	tcgetattr(fd,&oldtio); /* save current port settings */

	bzero(&newtio, sizeof(newtio));
	newtio.c_cflag = BAUDRATE | CS8 | CLOCAL | CREAD ; // | CRTSCTS ;
	newtio.c_iflag = IGNPAR | IGNBRK;
	newtio.c_oflag = 0;

	/* set input mode (non-canonical, no echo,...) */
	newtio.c_lflag = 0;

	newtio.c_cc[VTIME]    = 0;   /* inter-character timer unused */
	newtio.c_cc[VMIN]     = 1;   /* blocking read until 5 chars received */

	tcflush(fd, TCOFLUSH);
	tcflush(fd, TCIFLUSH);
	tcsetattr(fd,TCSANOW,&newtio);


	while (1) {
		printf("Enter command :\n");
		fgets(input, sizeof(input)-1, stdin);
		if ( strcmp(input, "q" ) == 0 ) {
			break;
		}
		for ( i = 0 ; i < 11; i++ ) {
			printf("send sample packet #%d\n", i);
			res = write(fd, sample_packet_list[i], strlen(sample_packet_list[i]));

			while (1) {       /* loop for input */
				res = read(fd, buf, sizeof(buf)-1);   /* returns after 5 chars have been input */
				if ( res < 0 ) { 
					continue;
				}
				buf[res]=0;               /* so we can printf... */
				printf("%s\n", buf);
				if ( strchr(buf, '$') ) {
					break;
				}
			}
		}

	}
	tcsetattr(fd,TCSANOW,&oldtio);

	return 0;
}

